home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cmln0985.arc / INTELHEX.PAS < prev    next >
Pascal/Delphi Source File  |  1986-02-27  |  6KB  |  166 lines

  1. program intelhex;
  2.            {This program will take a file that is in the format produced by
  3.             the CP/M dump utility and produce a file in intel hex format
  4.             suitable for use with the CP/M load command.
  5.             To use: Type intelhex   and respond to the prompts for file names.
  6.            Gary Sarff 10/03/84 BOX E Hood Center Norris City, Il 62869
  7.            }
  8.  
  9.  type  dumprec   = record
  10.                      addr  : array[1..4] of char;
  11.                      data  : array[1..16] of
  12.                                record
  13.                                  fill  : char;  {the blank between bytes}
  14.                                  item  : array[1..2] of char
  15.                                end;
  16.                    end; {dumprec}
  17.  
  18.        intelrec  = record
  19.                      linelength : array[1..2] of char;
  20.                      addr       : array[1..4] of char;
  21.                      zeroes     : array[1..2] of char;
  22.                      data       : array[1..16] of
  23.                                     array[1..2] of char;
  24.                      checksum   : array[1..2] of char;
  25.                    end; {intelrec}
  26.  
  27.   var
  28.         dumpline       : dumprec;
  29.         intelline      : intelrec;
  30.         dumpfile       : text buffered(512); {512 byte buffer to speed up}
  31.         intelfile      : text;               {processing. can be expanded}
  32.         infile,
  33.         outfile        : string;  {used to get actual file names from user,
  34.                                    adjust the type here as needed.}
  35.   function  hex_int(
  36.                      var  char_array   : array[0..0] of char;
  37.                           len_of_array : integer
  38.                    ):integer;
  39.                                 {This function returns the base 10 
  40.                                  representation of the hex characters in
  41.                                  the array char_array. [0..0] means the
  42.                                  pascal compiler should accept any variable
  43.                                  of type array of char as an argument since we
  44.                                  are passing only a fixed length address here.
  45.                                 }
  46.      var   i     : integer;
  47.            value : integer;
  48.  
  49.      begin {hex_int}
  50.        value:=0;
  51.        for i:=0 to len_of_array-1 do
  52.          case char_array[i] of
  53.           '0'..'9':value:=value*16+ord(char_array[i])-ord('0');
  54.           'A'..'F':value:=value*16+ord(char_array[i])-ord('A')+10
  55.          end; {case}
  56.        hex_int:=value
  57.      end; {hex_int}
  58.  
  59.   procedure int_hex(
  60.                     number       : integer;
  61.                     len_of_array : integer;
  62.                 var char_array   : array[0..0] of char
  63.             );
  64.                            {This procedure will take the base 10 integer in
  65.                             number and form a hex character representation of
  66.                             it having length len_of_array and place the 
  67.                             characters into the array char_array.
  68.                            }
  69.      var   i  : integer;
  70.            n  : integer;
  71.            ch : char;
  72.  
  73.      begin {int_hex}
  74.        for i:=len_of_array-1 downto 0 do
  75.            begin
  76.              n:=number mod 16;
  77.              case n of
  78.               0..9   : ch:=chr(ord('0')+n);
  79.               10..15 : ch:=chr(ord('A')+n-10);
  80.              end; {case}
  81.              char_array[i]:=ch;
  82.              number:=number div 16
  83.            end {for}
  84.      end; {int_hex}
  85.  
  86.   procedure checksum(var rec : intelrec);  {compute the 1 byte checksum for
  87.                                             rec and place it in rec.checksum}
  88.      var   i   : integer;
  89.            cks : integer;    {holds the checksum during the loop}
  90.            len : integer;    {length of rec}
  91.  
  92.      begin {checksum}
  93.        cks:=hex_int(rec.linelength,2);   {initialize checksum}
  94.        cks:=cks+hex_int(rec.addr,2)+hex_int(rec.addr,4)
  95.                -hex_int(rec.addr,2)*256;
  96.        len:=hex_int(rec.linelength,2);
  97.        for i:=1 to len do
  98.            cks:=cks+hex_int(rec.data[i],2);
  99.        cks:=255-(cks mod 256)+1;  {We need two's comp. of checksum. That is
  100.                                    what CP/M load program expects to find.}
  101.        int_hex(cks,2,rec.checksum);
  102.    end; {checksum}
  103.  
  104.   procedure init_files;
  105.      begin {init_files}
  106.        write("Enter the name of the input dump file -> ");
  107.        readln(infile);
  108.        reset(dumpfile,infile);
  109.        write("Enter the name of the output hex file -> ");
  110.        readln(outfile);
  111.        rewrite(intelfile,outfile);
  112.      end; {init_files}
  113.  
  114.   procedure line_read;   {Read a line from dumpfile into dumpline}
  115.      var  i : integer;
  116.  
  117.      begin
  118.        for i:=1 to 4 do     {needed because this pascal won't 
  119.                              read array of char}
  120.          read(dumpfile,dumpline.addr[i]);
  121.        for i:=1 to 16 do
  122.            with dumpline.data[i] do
  123.              read(dumpfile,fill,item[1],item[2]);
  124.        readln(dumpfile);
  125.      end; {line_read}
  126.  
  127.   procedure line_write;  {Write the line in intelline to the file intelfile}
  128.      var  i : integer;
  129.  
  130.      begin
  131.        with intelline do
  132.          write(intelfile,':',linelength,addr,zeroes);
  133.        for i:=1 to 16 do
  134.          write(intelfile,intelline.data[i]);
  135.        write(intelfile,intelline.checksum);
  136.        write(intelfile,chr(13),chr(10));  {standard CP/M line terminators}
  137.      end; {line_write}
  138.  
  139.   procedure move_line;  {Move the data in dumpline into intelline}
  140.      var  i : integer;
  141.  
  142.      begin
  143.        with intelline do
  144.          begin
  145.            linelength:='10';  {always except on last line}
  146.            addr:=dumpline.addr;
  147.            zeroes:='00';      {unknown why these are needed}
  148.            for i:=1 to 16 do
  149.              data[i]:=dumpline.data[i].item;
  150.          end {with}
  151.      end; {move_line}
  152.  
  153.   begin {main}
  154.     init_files;
  155.     while(not eof(dumpfile)) do
  156.       begin
  157.         line_read;
  158.         move_line;
  159.         checksum(intelline);
  160.         line_write
  161.       end; {while}
  162.     write(intelfile,':0000000000',chr(13),chr(10));
  163.     close(dumpfile);
  164.     close(intelfile)
  165.   end. {intelhex}
  166.